// myCourse_package/pages/historyCourse/historyCourse.ts
import {getHistoryCourse} from '../../../api/myCourse'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postData:{
      pageNum:1,
      pageSize:10,
    },
    total:0, //总页数
    moreLimt:false,//是否加载完成
    isfreshComplete:false, //是否刷新完成
    historyCourseList:[],
    tabList:[],
    show: false,
    actions: [
      {
        name: '近一周',
        value:'week'
      },
      {
        name: '近一月',
        value:'month'
      },
      {
        name: '近三月',
        value:'threeMonth'
      },
      {
        name: '近一年',
        value:'year'
      },
    ],
  },
    // 列表刷新课程
    loadPageEvent(data:any){
      const type = data.detail.type
      switch(type){
        case 'refresh':
          this.resetPage()
          this.getHistoryCourse()
          this.setData({
            isfreshComplete:true
          })
          break;
         case 'load':
          let postData = this.data.postData as any
          if(postData.pageNum * postData.pageSize < this.data.total){
            postData.pageNum  = postData.pageNum + 1
            this.setData({
              postData
            })
            this.getHistoryCourse()
          }else{
           this.setData({
            moreLimt: true
           })
          }
          break;
      }
      },
              // 重置页码
  resetPage(){
    let postData = this.data.postData as any
    postData.pageNum = 1
   this.setData({
    postData,
    myCourseList:[]
   })
  },
  // 更多筛选
  toMoreSelect(){
  this.setData({
    show:true
  })
  },
   //切换筛选条件
   handleChange(e:any){
    this.resetPage()
    let stuId:number = e.detail
    if(stuId === 0){
      const postData = this.data.postData as any
      delete postData.stuId
      this.setData({
        postData,
        historyCourseList:[]
      })
    }else{
      const newForm = {...this.data.postData,stuId}
      this.setData({
        postData:newForm,
        historyCourseList:[]
      })
    }
  
    this.getHistoryCourse()
    },
  // 关闭面板
  onClose(){
    this.setData({
      show:false
    })
  
  },
  onSelect(val:any){
    this.resetPage()
  const {value} = val.detail
  const dateList = wx.Utils.getStartAndEndDates(value)
 const {startDate,endDate} = dateList
 const postData = {...this.data.postData,startDate,endDate}
   this.setData({
     postData,
     historyCourseList:[]
   })
   this.getHistoryCourse()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  this.init()
  this.getHistoryCourse()

  },
  init(){
    const list = getApp().getMemberList().map((val:any)=>{
      return {
        id:val.stuId,
        label:val.userName
      }
    })
    list.unshift({
      id:0,
      label:'全部'
    })
    
    this.setData({
      tabList:list
    })
  },
  async getHistoryCourse(){
  const {code, data:res} = await getHistoryCourse(this.data.postData)
  if(code === 200){
    const {list,total} = res
  const historyCourseList = this.data.historyCourseList.concat(list)
   this.setData({
    historyCourseList,
    total
   })
  }

},

  //前去课程介绍页面
  courseDetail(event:any){
    const {title,stuid,serialid} =event.currentTarget.dataset
   wx.navigateTo({
     url:`/myCourse_package/pages/CourseIntroduction/CourseIntroduction?title=${title}&stuId=${stuid}&serialId=${serialid}`
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})